//
//  HRPlayManager.m
//  Hear
//
//  Created by mac on 2017/6/28.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRPlayManager.h"
#import <MediaPlayer/MediaPlayer.h>

#include <sys/types.h>
#include <sys/sysctl.h>
@interface HRPlayManager ()
@property (nonatomic, strong) AVPlayerItem   *currentPlayerItem;
@property (nonatomic) BOOL isLocalVideo; //是否播放本地文件
@property (nonatomic) BOOL isFinishLoad; //是否下载完毕
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@end
static HRPlayManager *_instance = nil;
NSString *itemArchivePath(){
    
    NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    return [pathList[0] stringByAppendingPathComponent:@"hearSound.sqlite"];//
}
@implementation HRPlayManager
{
    id _timeObserve;
}

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    
    return _instance;
}
- (instancetype)init{
    self = [super init];
    if (self) {
        [self.player addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
        // 支持后台播放
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
        // 激活
        [[AVAudioSession sharedInstance] setActive:YES error:nil];
        // 开始监控
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        NSURL *storeURL = [NSURL fileURLWithPath:itemArchivePath()];
        NSError *error = nil;
        
//        if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
//                                                       configuration:nil
//                                                                 URL:storeURL
//                                                             options:nil
//                                                               error:&error]){
//            
//            @throw [NSException exceptionWithName:@"OpenFailure"
//                                           reason:[error localizedDescription]
//                                         userInfo:nil];
//        }

    }
    return self;
}


@end
